package com.tarena.lbs.basic.web.service;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tarena.lbs.basic.web.dao.repository.AreaRepository;
import com.tarena.lbs.pojo.basic.po.AreaPO;
import com.tarena.lbs.pojo.basic.vo.AreaVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class AreaService {
    @Autowired
    private AreaRepository areaRepository;
    public List<AreaVO> getChildren(Integer parentId) {
        List<AreaPO> pos= getChildrenByParentId(parentId);

        List<AreaVO> vos = null;
        if (CollectionUtils.isNotEmpty(pos)) {
            vos=pos.stream().map(po -> {
                AreaVO vo = new AreaVO();
                BeanUtils.copyProperties(po,vo);
                return vo;
            }).collect(Collectors.toList());
        }
        return vos;
    }

    private List<AreaPO> getChildrenByParentId(Integer parentId) {
        QueryWrapper<AreaPO> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("parent_id",parentId);
        return areaRepository.list(queryWrapper);
    }

    public List<AreaVO> tree() {
        //1.查询所有 3000个区域数据 list 没有划分级别
        List<AreaPO> pos = areaRepository.list();
        //2.将pos 没有组织成树砖结构的list数据封装转化
        //pos2vos(pos);
        return assembleAreaTree(pos);
    }

    private List<AreaVO> assembleAreaTree(List<AreaPO> pos) {
        //1.pos所有行政区域 筛选出来 po的parentId=0
        List<AreaPO> provincePos = pos.stream().filter(po->po.getParentId().equals(0l)).collect(Collectors.toList());
        log.info("获取到省级列表:{}",provincePos);
        //2.先将省的pos转化成vo 在封装
        List<AreaVO> provinceVos = pos2vos(provincePos);
        //3.循环省的vos 使用id 筛选市级单位
        for (AreaVO provinceVo : provinceVos) {
            Long provinceVoId = provinceVo.getId();//循环每一个省 拿到省级的id
            //拿到这个省下边的市列表 转化vos 给省对象使用 children
            List<AreaPO> cityPos = pos.stream().filter(po -> po.getParentId().equals(provinceVoId)).collect(Collectors.toList());
            List<AreaVO> cityVos = pos2vos(cityPos);
            provinceVo.setChildren(cityVos);
            //循环市vo 从pos数据中筛选区数据
            for (AreaVO cityVo : cityVos) {
                Long cityId = cityVo.getId();
                List<AreaPO> areaPos = pos.stream().filter(po -> po.getParentId().equals(cityId)).collect(Collectors.toList());
                List<AreaVO> areaVoS = pos2vos(areaPos);
                cityVo.setChildren(areaVoS);
            }
        }
        return provinceVos;
    }

    private List<AreaVO> pos2vos(List<AreaPO> pos) {
        return pos.stream().map(po -> {
            AreaVO vo = new AreaVO();
            BeanUtils.copyProperties(po,vo);
            return vo;
        }).collect(Collectors.toList());
    }
}
